package com.feidian.helper.util;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

public class Utils {
	public static boolean isMobileNO(String phone) {
		Pattern p = Pattern
				.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");

		Matcher m = p.matcher(phone);

		return m.matches();
	}

	// 将时间戳转为字符串
	public static String getStringTime(String time) {
		String re_StrTime = null;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyy年MM月dd日   HH:mm:ss");
		long lcc_time = Long.valueOf(time);
		re_StrTime = simpleDateFormat.format(new Date(lcc_time));
		return re_StrTime;
	}

	/**
	 * @category 根据文件路径保存Bitmap
	 * @param filePath
	 * @param bitmap
	 * @return
	 */
	public static boolean save(String filePath, Bitmap bitmap) {
		if (bitmap != null) {
			File f = new File(filePath);
			try {
				FileOutputStream fOut = null;
				fOut = new FileOutputStream(f);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
				f.createNewFile();
				try {
					fOut.flush();
				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					fOut.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			return true;
		}
		return false;
	}

	/*
	 * 判断sdcard是否被挂载
	 */
	public static boolean hasSdcard() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}
	//得到工程存放图片的目录
	public static File getHelperFile(){
		File f = new File(Environment.getExternalStorageDirectory()
				+ "/helper");
		if (!f.exists())
			f.mkdirs();
		return f;
	}
}
